#endcoding:utf-8
class FactoryInfosController < ApplicationController
  before_action :logged_in?
  layout "user"
  def new
    @info = FactoryInfo.new
  end

  def create
    @info= FactoryInfo.new(info_params)
    if @info.save
      flash[:success] = "保存成功"
      redirect_to "/myinfo/"+uid
    else
      flash[:danger] = "保存失败"
      @errors = @info.errors.full_messages
      render "new"
    end
  end

  def edit
    @info = FactoryInfo.find_by_user_id(params[:id])
    if !@info
      redirect_to new_factory_info_path
    end
  end
  def update
    @info  = FactoryInfo.find(infoid)
    if @info.update(info_params)
      flash[:success] = "保存成功"
      redirect_to "/myinfo/"+uid
    else
      flash[:danger] = "保存失败"
      @errors = @info.errors.full_messages
      render "/editinfo"
    end
  end
  private
  def info_params
    params[:factory_info].store('user_id',uid)
    params.require(:factory_info).permit(:user_id,:name,:address,:phone)
  end
end
